package net.viralpatel.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class AddRoomAction extends ActionSupport {
	private String typeroom;
	private int floor;
	private String number;
	private int seat;
	private int status;
	private float price;
	private String description;
	String roomID;
	int type;
	String url = "jdbc:mysql://localhost:3306/";
	String dbName = "t10";
	String driverName = "com.mysql.jdbc.Driver";
	Connection con = null;
	ResultSet resultSet = null;
	String rootname = "root";
	String passwdroot = "";

	public boolean checkRoom() throws Exception {
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url + dbName, rootname,
					passwdroot);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		String selectCheck = "SELECT * FROM room WHERE roomID= ?";
		PreparedStatement prepStmt = con.prepareStatement(selectCheck);
		
		if (typeroom.equals("A") || typeroom.equals("B")
				|| typeroom.equals("C") || typeroom.equals("D")) {
			roomID = ""+1+typeroom+floor+number;
			type=1;
		}else{
			roomID = ""+2+typeroom+floor+number;
			type=2;
		}
		
		prepStmt.setString(1,roomID);
		resultSet = prepStmt.executeQuery();
		resultSet.last();
		int rowCol = resultSet.getRow();
		if (rowCol == 1) {
			con.close();
			return true;
		} else {
			con.close();
			return false;
		}
	}

	public String execute()throws Exception {
		if(checkRoom()==true){
			addActionError(getText("error.addRoom"));
			return ERROR;
		}else{
			try {
				Class.forName(driverName);
				con = DriverManager.getConnection(url + dbName,rootname,passwdroot);
			} catch (ClassNotFoundException e) {
				System.out.println(e.getMessage());	
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			String selectLogin = "Insert into room (roomID,roomType1,roomType2,seat,floor," +
					"roomNumber,price,description,status) value (?,?,?,?,?,?,?,?,?)";
			PreparedStatement prepStmt = con.prepareStatement(selectLogin);
			System.out.println(roomID);
			prepStmt.setString(1,roomID );
			prepStmt.setInt(2, type);
			prepStmt.setString(3, typeroom);
			prepStmt.setInt(4, seat);
			prepStmt.setInt(5, floor);
			prepStmt.setString(6, number);
			prepStmt.setFloat(7,price);
			prepStmt.setString(8,description );
			prepStmt.setInt(9, status);
			int res = prepStmt.executeUpdate();
			if (res == 0) {
				addActionError(getText("error.addRoom"));
				con.close();
				return ERROR;
			} else {
				con.close();
				return SUCCESS;
			}
		}
	}

	public void setTyperoom(String typeroom) {
		this.typeroom = typeroom;
	}

	public String getTyperoom() {
		return typeroom;
	}

	public void setFloor(int floor) {
		this.floor = floor;
	}

	public int getFloor() {
		return floor;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getNumber() {
		return number;
	}

	public void setSeat(int seat) {
		this.seat = seat;
	}

	public int getSeat() {
		return seat;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getStatus() {
		return status;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public float getPrice() {
		return price;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

}
